import {Component, OnInit, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {ModalDirective} from 'ngx-bootstrap';
import {TagList, TagInfo, AddTag, EditTag} from './tag';
import {CouponsService} from '../../../lib/service/admin/coupons.service';

@Component({
    selector: 'app-coupons-tag',
    templateUrl: './tag.component.html',
    styleUrls: ['./tag.component.scss'],
    providers: [CouponsService]
})
export class TagComponent implements OnInit {

    totalItems: number;
    currentPage = 1;
    itemsPerPage = 12;
    name: string;
    model: string;
    orderField: string;
    orderSort: string;
    type: string;
    message: string;
    isShow = false;
    tagList = new TagList();
    tagInfo = new TagInfo();
    addTag = new AddTag();
    editTag = new EditTag();

    @ViewChild('addModal') public addModal: ModalDirective;
    @ViewChild('editModal') public editModal: ModalDirective;

    constructor(
        private router: Router,
        private couponsService: CouponsService
    ) {
        this.name = '';
        this.model = '';
        this.orderField = '';
        this.orderSort = '';
    }

    ngOnInit() {
        this.getTagList();
    }

    getTagList() {
        this.couponsService.getCouponsTagList(this.currentPage, this.itemsPerPage).subscribe(
            data => {
                if(data.code === 2000) {
                    this.tagList.list = data.data.data;
                    this.totalItems = data.data.total;
                }
            }
        );
    }

    getTagInfo(obj) {
        this.editTag.card_coupons_tag_uuid = obj.card_coupons_tag_uuid;
        this.editTag.name = obj.card_coupons_tag_name;
        console.log(this.editTag);
    }

    addCouponsTagSubmit() {
        this.couponsService.addCouponsTag(this.addTag).subscribe(
            data => {
                if(data.code === 2000) {
                    this.type = 'success';
                    this.message = '添加成功';
                    this.isShow = true;
                    this.addModal.hide();
                    this.getTagList();
                }
            }
        );
    }

    editCouponsTagSubmit() {
        console.log(this.editTag);
        this.couponsService.editCouponsTag(this.editTag).subscribe(
            data => {
                if(data.code === 2000) {
                    this.type = 'success';
                    this.message = '修改成功';
                    this.isShow = true;
                    this.editModal.hide();
                    this.getTagList();
                }
            }
        );
    }

    pageChanged(event: any): void{
      window.scrollTo(0,0);
      this.currentPage = event.page;
      this.getTagList();
      //this.getTemplateTypeList(event.page,this.searchData)
    }

    promptIsShow(event: any): void{
        this.isShow = event;
    }
}
